package servlet;

import constant.ExamConstant;
import dao.DeptDao;
import model.Dept;
import service.DoctorService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/registerDoctor")
public class RegisterDoctor extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 设置请求编码格式
        req.setCharacterEncoding("utf-8");
        // 设置响应编码格式
        resp.setContentType("text/html;charset=utf-8");
        //获取请求信息
        String doctorId=req.getParameter("doctorId");
        String doctorName=req.getParameter("doctorName");
        String deptName=req.getParameter("deptName");
        Integer deptId= Integer.valueOf(req.getParameter("deptId"));
        String doctorSex=req.getParameter("doctorSex");
        String doctorPwd=req.getParameter("doctorPwd");
        // 处理请求信息
        // 获取业务层对象
        DoctorService doctorService = new DoctorService();
        boolean flag = doctorService.regist(Integer.valueOf(doctorId),doctorName,doctorPwd,Integer.valueOf(doctorSex),deptId);
        // 响应处理结果
        if (!flag){// 用户名重复
            req.setAttribute("error","医生账号已存在，请重新填写");
            req.getRequestDispatcher("./registerDoctor.jsp").forward(req,resp);
        } else {
            resp.sendRedirect(ExamConstant.HOME_PAGE_LOGIN);
        }
    }
}
